package com.zhengtoudai.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.zhengtoudai.R;
import com.zhengtoudai.base.MvpActivity;
import com.zhengtoudai.ui.bean.MessageCenterModel;
import com.zhengtoudai.ui.adapter.MessageCenterAdapter;
import com.zhengtoudai.ui.presenter.MessageCenterPresenter;
import com.zhengtoudai.ui.view.MessageCenterView;
import com.zhengtoudai.util.AbToastUtil;
import com.zhengtoudai.view.swipetoloadlayout.base.OnLoadMoreListener;
import com.zhengtoudai.view.swipetoloadlayout.base.OnRefreshListener;
import com.zhengtoudai.view.swipetoloadlayout.base.SwipeToLoadLayout;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * 我的消息
 */
public class MessageCenterActivity extends MvpActivity<MessageCenterPresenter> implements MessageCenterView, OnRefreshListener, OnLoadMoreListener {

    @Bind(R.id.swipe_target)
    RecyclerView swipeTarget;
    @Bind(R.id.refresh)
    SwipeToLoadLayout refresh;
    private MessageCenterAdapter adapter;

    private Activity mActivity;
    private int pageNum;
    private int totalNum;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_message_center);
        ButterKnife.bind(this);
        mActivity=MessageCenterActivity.this;
        super.onCreate(savedInstanceState);
    }

    @Override
    protected MessageCenterPresenter createPresenter() {
        return new MessageCenterPresenter(this);
    }

    @Override
    protected void initTitle() {
        titleUtil.setTitle("我的消息");
    }

    @Override
    protected void initView() {
        refresh.setOnRefreshListener(this);
        refresh.setOnLoadMoreListener(this);
        swipeTarget.setLayoutManager(new LinearLayoutManager(context));
        adapter = new MessageCenterAdapter();
        swipeTarget.setAdapter(adapter);
        status.showLoading();
        refresh.post(new Runnable() {
            @Override
            public void run() {
                refresh.setRefreshing(true);
            }
        });
        autoLoading(refresh, swipeTarget);
    }
    @Override
    public void onLoadMore() {
        if(pageNum<totalNum){
            int current=pageNum+1;
            mvpPresenter.loadData(mActivity,"10",current+"");
        }else{
            refresh.setLoadingMore(false);
            AbToastUtil.showToast(context,"已无更多数据");
        }

    }

    @Override
    public void onRefresh() {
        adapter.clearData();
        mvpPresenter.loadData(mActivity,"10","1");
    }

    @Override
    public void getDataSuccess(MessageCenterModel model) {
//        AbToastUtil.showToast(context,model.getMessage());
        MessageCenterModel.MessageCenterBean mInfo=model.getResult();
        if (mInfo.getList()==null||mInfo.getList().size()==0){
            status.showNoData("暂无数据");
        }else{
            totalNum=Integer.valueOf(mInfo.getPageInfo().getTotalNum());
            pageNum=Integer.valueOf(mInfo.getPageInfo().getPageNum());
            status.removeView();
            adapter.addData(mInfo.getList());
        }
    }

    @Override
    public void getDataFail(int code) {
        if (refresh.isRefreshing()){
            refresh.setRefreshing(false);
        }else{
            refresh.setLoadingMore(false);
        }
        status.removeView();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {
        if (refresh.isRefreshing()){
            refresh.setRefreshing(false);
        }else{
            refresh.setLoadingMore(false);
        }
    }

}
